﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UpdateDateByAc.Entity;


namespace UpdateDateByAc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.MaximizeBox = false;


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "") {
                label8.Text = "条码号不能为空";
                
            }
            if (ValidateInput()) {
                var Rq = Comm.Getrqtime(textBox1.Text);
                foreach (var cc in Rq)
                {
                    textBox2.Text = cc.Cjtime.ToString("yyyy-MM-dd HH:mm:ss:fff");
                    textBox3.Text = cc.Cyrq.ToString("yyyy-MM-dd HH:mm:ss:fff");
                    textBox4.Text = cc.Bgrq.ToString("yyyy-MM-dd HH:mm:ss:fff");
                }
            }
            else
            {
                label8.Text = "条码不符合规定";
            }
        }


        //点击查询过滤
        public bool ValidateInput()
        {
            if (textBox1.Text.Trim() == "")
            {

                return false;
            }

            else if (textBox1.Text.Length < 12)
            {

                return false;
            }
            return true;
        }

        //时间修改过滤
        public bool DateValidateInput()
        {
            if (textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "" || textBox4.Text.Trim() == "" )
            {

                return false;
            }
            return true;
        }



        private void button3_Click(object sender, EventArgs e)
            {
               if (textBox1.Text == "")
                    {
                 label8.Text = "条码号不能为空";
                    }

            if (ValidateInput()) { 

            var a = Comm.UpdateBg(textBox1.Text);
                if (a > 1 || a == 1)
                {
                    button3.Text = "重新生成成功";
                }
                else
                {
                    button3.Text = "当前条码不存在或者未出结果。";
                }
            }
            else
            {
                label8.Text = "条码不符合规定";
            }
        }

            private void button2_Click(object sender, EventArgs e)
            {
            if (DateValidateInput()) {
                DateTime cjRq =Convert.ToDateTime(textBox2.Text.Substring(0, 19));
                DateTime cyRq = Convert.ToDateTime(textBox3.Text.Substring(0, 19));
                DateTime bgRq = Convert.ToDateTime(textBox4.Text.Substring(0, 19));
                String ybid = textBox1.Text;
                var ac = Comm.UpdateTime(cjRq, cyRq, bgRq, ybid);
                if (ac > 1 || ac == 1)
                {
                    label8.Text = "修改成功";
                }else
                {
                    label8.Text = "修改未成功";
                }
                //static int UpdateTime(DateTime cjRq, DateTime cyRq, DateTime bgRq, String ybid)

            }
            else
            {
                label8.Text = "时间格式不符合规定";
            }



        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
       
            if (Comm.GetHospital(textBox5.Text.Trim()).Count > 0 && textBox5.Text !="")
            {
                foreach (var cc in Comm.GetHospital(textBox5.Text.Trim()))
                {
                    button4.Text = "医院已存在";
                    textBox6.Text = cc.Mc;
                     if (textBox6.Text.Trim() == "")
                {
                    button4.Text = "医院不能为空";
                }
                }
            }            
            else
            {
                if (textBox6.Text.Trim() == "")
                {
                    button4.Text = "医院不能为空";
                }
                else
                {
                    if (Comm.InsertHos(textBox5.Text.Trim(), textBox6.Text.Trim()) == 1)
                    {
                        button4.Text = "添加成功";
                    }
                    else
                    {
                        button4.Text = "添加失败";
                    }

                }
             

            }

 
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(WindowState == FormWindowState.Maximized)
            {
                WindowState = FormWindowState.Normal;

                this.Activate();


                this.ShowInTaskbar = true;

                notifyIcon1.Visible = false;
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if(WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;

                notifyIcon1.Visible = true;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //if (MessageBox.Show("是否确认退出程序？", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            //{
            //    // 关闭所有的线程

                
            //    this.Dispose();
            //    this.Close();
            //}
            //else
            //{
            //    e.Cancel = true;
            //}
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void contextMenuStrip1_MouseDown(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Maximized;
            notifyIcon1.Visible = false;
            this.ShowInTaskbar  = true;
            this.TopMost = true;
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (CopyAdd())
            {
                if(Comm.InsertUser(textBox7.Text.Trim(), textBox8.Text.Trim(),textBox9.Text.Trim()) == 1)
                {
                    label12.Text = "复制添加成功";
                }
                else
                {
                    label12.Text = "复制添加失败";
                }


            }
            else
            {
                label12.Text= "请检查人员格式";
            }

           


            //INSERT INTO xt_user(logid, username, password, power, status, department, tsxmsh, fshuncheck, zxid, gid, yqqx, ryqqx, djz, zyzqx, qdlis_login, qdlis_operate, Roletype, bbqr, bbjj, jyjs) VALUES('9999', '测试', 'ceshi', '3', '1', '01051005', '1', '1', 'ty', 156, ',TS,DX800,XP,M2000,E601,', ',TS,DX800,XP,M2000,E601,', '张琪', ',1,16,18,22,25,', '1', '1', '12', '1', '1', '1')
        }


        //复制增加过滤
        public  bool CopyAdd()
        {
            if (textBox7.Text =="" || textBox8.Text == "")
            {
                label12.Text = "请检查人员格式";
                return false;
            }
            else if (textBox9.Text =="")
            {
                label12.Text = "请选择模仿对象";
                return false;
            }
            return true;
        }

        private void button6_Click(object sender, EventArgs e)
        {

            if(!Comm.CheckStringChineseUn(textBox10.Text) && textBox10.Text.Length ==6 )//获取医院密码
            {
                foreach(var aa in Comm.GetHospital(textBox10.Text)){
                    textBox11.Text = aa.Password;
                    Clipboard.SetText(aa.Password);
                }

            }
            else
            {
                if (Comm.GetPass(textBox10.Text.Trim(), textBox10.Text.Trim()).Count ==0)
                {
                    label14.Text = "没有当前用户";
                }

                try { 

                     
                foreach(var aa in Comm.GetPass(textBox10.Text.Trim(), textBox10.Text.Trim()))
                {
                    if(aa == null)
                    {
                        label14.Text = "没有当前用户";
                    }
                    else
                    {
                        if(aa.accountLock == "0")
                            {
                                textBox11.Text = aa.Password;
                                Clipboard.SetText(aa.Password);

                                checkBox1.Checked = false; 
                            }
                            else {
                                textBox11.Text = aa.Password;
                                
                                checkBox1.Checked = true;
                                Clipboard.SetText(aa.Password);

                            }
                        
                        
                    }
                    
                }
                }
                catch (Exception)
                {
                    label14.Text = "没有当前用户或为空";
                }

            }



        }

        private void button8_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process[] processList = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process process in processList)
            {
                if (process.ProcessName.ToUpper() == "LIMS")
                {
                    MessageBox.Show("已结束");
                    process.Kill(); //结束进程
                }
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
           
           if( Comm.DellInputDate(textBox12.Text.Trim()) == 1)
            {
                button9.Text = "已经删除";
            }
            else
            {
                button9.Text = "删除报错";
            }
        }
        
    }

      
 }
  
